    //
//  WebViewController.m
//  HanBlog
//
//  Created by imac.bmw on 10-10-12.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "WebViewController.h"
#import <MessageUI/MessageUI.h>
#import <MessageUI/MFMailComposeViewController.h>

@implementation WebViewController
@synthesize htmlString,blogTitle;


// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
	[super loadView];
	self.view.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.46];	
		
	UIBarButtonItem *share = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemReply target:self action:@selector(sendMail)];	
	self.navigationItem.rightBarButtonItem = share;
	
	[share release];
	
	
	web = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, 320, 420)];
	[web setBackgroundColor:[UIColor colorWithRed:1 green:1 blue:1 alpha:0.36]];
	[web setOpaque:NO];

	
	//网页开始加载内容
	[web loadData:[htmlString dataUsingEncoding:NSUTF8StringEncoding]
			   MIMEType:@"text/html"
	   textEncodingName:@"UTF-8"
				baseURL:nil];
	
	
	[self.view addSubview:web];
	
		
}

-(void)sendMail {

	Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
	if (mailClass != nil)
	{
		// We must always check whether the current device is configured for sending emails
		if ([mailClass canSendMail])
		{
			[self displayComposerSheet];
		}
		else
		{
			[self launchMailAppOnDevice];
		}
	}
	else
	{
		[self launchMailAppOnDevice];
	}
	
}

-(void)displayComposerSheet 
{
	MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
	picker.mailComposeDelegate = self;
	
	[picker setSubject:self.blogTitle];
	
	
	// Set up recipients
	//NSArray *toRecipients = [NSArray arrayWithObject:@""]; 
//	NSArray *ccRecipients = [NSArray arrayWithObjects:@"",  nil]; 
//	NSArray *bccRecipients = [NSArray arrayWithObject:@""]; 
//	
//	[picker setToRecipients:toRecipients];
//	[picker setCcRecipients:ccRecipients];	
//	[picker setBccRecipients:bccRecipients];
	
	// Attach an image to the email
	//NSString *path = [[NSBundle mainBundle] pathForResource:@"rainy" ofType:@"png"];
//    NSData *myData = [NSData dataWithContentsOfFile:path];
//	[picker addAttachmentData:myData mimeType:@"image/png" fileName:@"rainy"];
	
	// Fill out the email body text
	NSString *emailBody = self.htmlString;
	[picker setMessageBody:emailBody isHTML:YES];
	
	[self presentModalViewController:picker animated:YES];
    [picker release];
}

- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error 
{	
	//message.hidden = NO;
//	// Notifies users about errors associated with the interface
//	switch (result)
//	{
//		case MFMailComposeResultCancelled:
//			message.text = @"Result: canceled";
//			break;
//		case MFMailComposeResultSaved:
//			message.text = @"Result: saved";
//			break;
//		case MFMailComposeResultSent:
//			message.text = @"Result: sent";
//			break;
//		case MFMailComposeResultFailed:
//			message.text = @"Result: failed";
//			break;
//		default:
//			message.text = @"Result: not sent";
//			break;
//	}
//	[self dismissModalViewControllerAnimated:YES];
	NSLog(@"mail success!");
	[self dismissModalViewControllerAnimated:YES];
}


-(void)launchMailAppOnDevice
{
	NSString *recipients = @"mailto:first@example.com?cc=second@example.com,third@example.com&subject=Hello from California!";
	NSString *body = @"&body=It is raining in sunny California!";
	
	NSString *email = [NSString stringWithFormat:@"%@%@", recipients, body];
	email = [email stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
	
	[[UIApplication sharedApplication] openURL:[NSURL URLWithString:email]];
}

-(void)dissView:(id)sender {
	
	
	[self dismissModalViewControllerAnimated:YES];
}


/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
}
*/

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
	[blogTitle release];
	[htmlString release];
    [super dealloc];
}


@end
